import java.io.*;
import java.net.*;

public class VLCControler{
	Socket socket = null;
    static PrintWriter out = null;
    static BufferedReader in = null;
	BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
   	String serveraddress="127.0.0.1";
	public VLCControler(String path){
		try {
            socket = new Socket(serveraddress, 1234);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		System.out.println("Connected");
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            try {
        		System.out.println("abrindo");
    			Runtime rt = Runtime.getRuntime();
    			//String[]callAndArgs= {"\"C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe\" -vvv -I rc "+"\""+ b +"\""+" --sout \"#transcode{vcodec=h264,vb=800,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=44100}:std{access=http,mux=ts,dst=:8080/teste}\""};
    			String callAndArgs= "\""+ path + "\" -I rc ";
    			//String[]callAndArgs= {"\"C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe\" -vvv -I rc \"C:\\video.avi\" --sout \"#transcode{vcodec=h264,vb=800,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=44100}:std{access=http,mux=ts,dst=192.168.1.101:8080/teste}\""};
                Process p=rt.exec(callAndArgs);
                p.getInputStream().close(); p.getOutputStream().close(); p.getErrorStream().close();
                socket = new Socket(serveraddress, 1234);
                out = new PrintWriter(socket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println("abriu");
                //System.out.println(Arrays.toString(callAndArgs));
            }
    		
            catch (IOException b) {
                b.printStackTrace();
            }
        }
	}
	
	
	
    public void comando(String cmd) {
                        
		//System.out.println(cmd); //Uncomment to debug
			out.println(cmd);
    }
}